<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
		<script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="/js/plugins/jrender/jrender.js"></script>
		<script type="text/javascript">
            $(function () {
                // render渲染数据
                var user = JSON.parse(sessionStorage.getItem("user"));
                $(".info").renderValues(user);
                // 选择器渲染背景图
                $(".myBg").css("background-image", "url('"+user.coverImgUrl+"')");

                // 给保存按钮绑定点击事件并把更新的用户信息发PUT请求到服务器
				$("#saveBtn").click(function () {
					$.ajax({
						url:"/users/"+user.id,
						data:$("#infoForm").serialize(),
						type:"PUT",
						success:function (data) {
						    // 1.更新本地缓存
							data.data.email = user.email;
							sessionStorage.setItem("user",JSON.stringify(data.data));
                            $(document).dialog({
                                type:"confirm",
                                style:"IOS",
                                titleShow:false,
                                content:"更新成功",
                                buttons:[{
                                    name:"返回个人主页",
                                    callback:function () {
                                        // 2.回到个人信息界面
                                        location.href = "/mine/profiles.html";
                                    }
                                }]
                            });
                        }
					});
                });
				var flag = true;

				// 处理图片上传
				$("#headBtn,#headImg").click(function () {
				    flag = true;
					// 弹出文件上传的选择器
					$("#imgForm input[name='pic']").click();

                });

                // 背景图片
                $("#coverBtn").click(function () {
                    flag = false;
                    // 弹出文件上传的选择器
                    $("#imgForm input[name='pic']").click();
                });

                // 设置文件上传值改变事件

				$("#imgForm input[name='pic']").change(function () {
					// 判断是否有选择图片
					if($("#imgForm input[name='pic']").val()){
						// 使用ajax提交图片上传的表单
						$("#imgForm").ajaxSubmit(function (data) {
                            if (flag) {
								// 回显到页面 1. 头像 2.隐藏域
								$("#headImg").attr("src",data.url);
								$("#infoForm input[name='headImgUrl']").val(data.url);
							} else {
                                // 回显到页面 1. 头像 2.隐藏域
                                $(".myBg").css("background-image", "url('"+data.url+"')");
                                $("#infoForm input[name='coverImgUrl']").val(data.url);
							}
						});
					}
					// 清空
                    $("#imgForm input[name='pic']").val();
				});



            });
		</script>
	</head>

	<body>
		<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" name="pic" style="display: none;" accept="image/*"/>
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" >
					<i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i>
				</div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
				<!--不需要再传id了，因为请求上面已经给了-->
				<!--<input type="hidden" name="id" >-->
				<input type="hidden" name="headImgUrl" render-value="headImgUrl">
				<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
				<label>昵称</label>
				<input type="text" name="nickName" class="form-control" render-value="nickName"><br/>
				<label>性别</label>
				<select class="form-control" name="gender" render-value="gender">
					<option value="-1">保密</option>
					<option value="0">男</option>
					<option value="1">女</option>
				</select><br/>
				<label>地区</label>
				<input type="text" name="place" class="form-control" render-value="place"><br/>
				<label>签名</label>
				<textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>

	</body>
</html>
